#!/bin/bash
lines=81


echo "解压安装包"
if [ -d "./nodes" ] #判定是否存在老的程序目录如是则删除
then
rm -fr nodes
fi
tail -n +$lines "$0" > nodes.tar.gz #提取程序
#xz -d nodes.tar.xz
tar -xvf nodes.tar.gz #解压程序

echo "拷贝节点到ros2_ws/src"
if [ ! -d ~/ros2_ws/src/ ]
then
mkdir -p ~/ros2_ws/src/ #创建程序存放目录
fi

cp -fr ./nodes/src/* ~/ros2_ws/src/ #拷贝程序

echo "拷贝树"
if [ ! -d ~/ros2_ws/tree/ ]
then
mkdir -p ~/ros2_ws/tree/ #创建行为树xml存放目录
fi
cp -fr ./nodes/tree/* ~/ros2_ws/tree/ #拷贝行为数xml文件

echo "拷贝DDS2ROS"
if [ ! -d ~/DDS2ROS/ ]
then
mv ./nodes/DDS2ROS ~/  #拷贝DDS2ROS程序
fi

#------------------------------------------------------------------------------------------------------------------------------------

if [ ! -d ~/droneyee_fly/ ]
then
echo "创建启动脚本路径"
mkdir -p ~/droneyee_fly
fi
if [ ! -f ~/droneyee_fly/realfly_startup.sh ]
then
echo "拷贝启动及cpu gpu频率脚本"
cp ./nodes/realfly_startup.sh ~/droneyee_fly/realfly_startup.sh #拷贝全部程序的启动脚本（同时此脚本为开机启动的脚本）
cp ./nodes/check_restart.sh ~/droneyee_fly/check_restart.sh     #拷贝全部程序的检测脚本（检测是否有程序推出运行，如退出则重启该程序）
cp ./nodes/max_gpu_freq.sh ~/droneyee_fly/max_gpu_freq.sh
cp ./nodes/max_cpu_freq.sh ~/droneyee_fly/max_cpu_freq.sh
fi

#------------------------------------------------------------------------------------------------------------------------------------

if [ ! -d ~/distributed-packaging-script/ ]
then
echo "创建打包脚本路径"
mkdir -p ~/distributed-packaging-script
fi

if [ ! -f ~/distributed-packaging-script/distribution-nodes-pack.sh ]
then
echo "拷贝打包脚本"
cp ./nodes/distribution-nodes-pack.sh ~/distributed-packaging-script/distribution-nodes-pack.sh    #拷贝程序打包脚本
cp ./nodes/distribution-nodes-install.sh ~/distributed-packaging-script/distribution-nodes-install.sh #拷贝程序安装脚本
cp ./nodes/packages-*.txt ~/distributed-packaging-script/ #拷贝程序列表文件
fi
#-------------------------------------------------------------------------------------------------------------------------------------
if [ ! -f ~/ros2_ws/mavros_param_1.yaml ]
then
echo "拷贝mavros配置文件"

cp ./nodes/mavros_param_1.yaml ~/ros2_ws/mavros_param_1.yaml
fi

#-------------------------------------------------------------------------------------------------------------------------------------
#删除安装包中解压内容及压缩包
rm -fr nodes
rm nodes.tar.gz

echo "安装完成"
exit 0
